package com.xdh.zzqh.store

import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.xdh.zzqh.app.AppContext
import com.xdh.zzqh.utils.runWithDb


private class Database : SQLiteOpenHelper(AppContext, "data2.db", null, 2) {

    var updateFlag = false
        private set

    override fun onCreate(db: SQLiteDatabase?) {

    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        updateFlag = true
    }
}

object SqliteConn {

    private val db = Database()
    val conn: SQLiteDatabase = db.writableDatabase

    suspend fun init() {
        runWithDb {
            if (db.updateFlag) {
                conn.execSQL(Product.DROP_SQL)
            }
            conn.execSQL(Product.CREATE_SQL)
            conn.execSQL(User.CREATE_SQL)
        }

    }
}

